import request from '@/utils/request'
import { otherFiltersItem } from '@/utils'
import { Message } from 'element-ui'
function handleResult(id = true) {
  return res => {
    if (res.data.records.length === 0) {
      Message.error('没有查询到来源数据！')
      return Promise.reject('')
    }
    if (id) {
      return res.data.records[0].id
    } else {
      return res.data.records[0]
    }
  }
}
// 获取申请数据id
function getShopDesignPage(constCode) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('constCode', constCode, '=')]))
  return request({
    url: '/api/v1/shopDesign/getShopDesign/page',
    method: 'POST',
    data
  }).then(handleResult())
}
// 获取设计变更id
function getShopDesignChangePage(changeCode) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('changeCode', changeCode, '=')]))
  return request({
    url: '/api/v1/shopDesignChange/getShopDesignChange/page',
    method: 'POST',
    data
  }).then(handleResult())
}
// 获取图纸id
export function getPicturePage(picTaskCode) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('picTaskCode', picTaskCode, '=')]))
  return request({
    url: '/api/v1/shopDesignPicture/getPicture/page1',
    method: 'POST',
    data
  }).then(handleResult(false))
}
// 获取测绘id
function queryByTermPage(surveyBatchCode) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('surveyBatchCode', surveyBatchCode, '=')]))
  return request({
    url: '/api/v1/shopSurvey/queryByTerm',
    method: 'POST',
    data
  }).then(handleResult())
}
// 获取异常id
function shopExceptionPage(exceptionCode) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([otherFiltersItem('exceptionCode', exceptionCode, '=')]))
  return request({
    url: '/api/v1/shopException/page',
    method: 'POST',
    data,
    headers: {
      'Permission-Percode': 'exceptionmanage',
      'Permission-App': 'slm'
    }
  }).then(handleResult())
}
export async function getPath(type, v) {
  const t = {
    query: {
      target: 'detail'
    }
  }
  switch (type) {
    case 'DESIGN_APPLICATION':// 设计申请 门店建设项目号 设计申请详情
      t.path = '/ShopDesign'
      await getShopDesignPage(v).then(id => { t.query.id = id })
      break
    case 'DESIGN_CHANGE':// 设计变更 门店设计变更申请号  设计变更申请详情
      t.path = '/ShopDesignChange'
      await getShopDesignChangePage(v).then(id => { t.query.id = id })
      break
    case 'DRAWING_TRIGGER':// 图纸触发 图纸任务号 图纸任务详情
      t.path = '/DrawingsManagement'
      await getPicturePage(v).then(d => { t.query.id = d.id; t.query.picTaskCode = d.picTaskCode; t.query.designRange = d.designRange; t.query.picTaskName = d.picTaskName; t.query.designerCode = d.designerCode; t.query.designerName = d.designerName; t.query.picType = d.picType })
      break
    case 'REPEATED_MAPPING':// 重复测绘 测绘任务号 测绘详情
      t.path = '/storeMapping'
      await queryByTermPage(v).then(id => { t.query.id = id })
      break
    case 'RESURVEY':// 门店复尺 测绘任务号 测绘详情
      t.path = '/storeMapping'
      await queryByTermPage(v).then(id => { t.query.id = id })
      break
    case 'DESIGN_PAUSE':// 设计暂停 异常单号 异常详情
      t.path = '/storeConstruction/exceptionManage'
      await shopExceptionPage(v).then(id => { t.query.id = id })
      break
    case 'DESIGN_REGAIN':// 设计恢复 异常单号 异常详情
      t.path = '/storeConstruction/exceptionManage'
      await shopExceptionPage(v).then(id => { t.query.id = id })
      break
    case 'ARTIFICIAL':// 手工新增 无
      return null
    default:
      break
  }
  return t
}
